<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\DB;

class IndexController extends Controller
{
    /**
     * 获取品牌车系
     *
     * @return array
     */

    public function index() {
        $brand = DB::table('cars')->where('type','1')->select('id', 'car', 'p_id')->get();
        $series = DB::table('cars')->where('type','2')->get();
        $brand = json_decode(json_encode($brand),true);
        $series = json_decode(json_encode($series),true);
        foreach ($brand as $index => $value) {
            $p_id = $value['id'];
            foreach ($series as $item) {
                if($item['p_id'] == $p_id) {
                    $brand[$index]['label'] = $value['car'];
                    $brand[$index]['value'] = $value['id'];
                    $brand[$index]['children'][] = ['value'=>$item['id'], 'label'=>$item['car']];                    
                } else {
                    $brand[$index]['label'] = $value['car'];
                    $brand[$index]['value'] = $value['id'];
                }
            }
        }
        return $brand;

        // $brand = DB::table('cars')->where('type','1')->select('id', 'car')->get();
        // $brand = json_decode(json_encode($brand),true);
        // foreach ($brand as $key => $value) {
        //     $series = DB::table('cars')->where([['type','2'],['p_id',$value['id']]])->select('id', 'car')->get();
        //     $brand[$key]['children'] = $series;
        // }

        // return $brand;
    }

    public function cardata() {
        $brand = DB::table('cars')->where('type','1')->get();
        $series = DB::table('cars')->where('type','2')->get();
        $brand = json_decode(json_encode($brand),true);
        $series = json_decode(json_encode($series),true);
        foreach ($brand as $index => $value) {
            $p_id = $value['id'];
            foreach ($series as $item) {
                if($item['p_id'] == $p_id) {
                    unset($brand[$index]['id']);
                    unset($brand[$index]['p_id']);
                    unset($brand[$index]['type']);
                    unset($brand[$index]['car']);
                    $brand[$index]['label'] = $value['car'];
                    $brand[$index]['value'] = $value['id'];
                    $brand[$index]['children'][] = ['value'=>$item['id'], 'label'=>$item['car']];                    
                } else {
                    unset($brand[$index]['id']);
                    unset($brand[$index]['p_id']);
                    unset($brand[$index]['type']);
                    unset($brand[$index]['car']);
                    $brand[$index]['label'] = $value['car'];
                    $brand[$index]['value'] = $value['id'];
                }
            }
        }
        return $brand;
    }

    public function car() {
        $username = $_POST['username'];
        
        if ($username != 'mac') {
            return ['msg' => '参数错误'];
        }

        $data = DB::table('cars as brand')
                ->leftJoin('cars as system', 'brand.id', '=', 'system.p_id')
                ->leftJoin('cars_price as price', 'system.id', '=', 'price.id')
                ->where('system.type', 2)
                ->select('brand.id as brand_id', 'brand.car as brand_name', 
                'system.id as system_id', 'system.car as system_name', 'price.max_price', 'price.min_price')
                ->get();
        
        $data = json_decode(json_encode($data), true);

        $count = DB::select('select error.id, count(error.id) as count 
                                    from website_price_errors as error 
                                    LEFT JOIN cars_price as price on error.id = price.id 
                                    where error.maxprice > price.max_price or error.minprice < price.min_price 
                                    group by error.id');
        $count = json_decode(json_encode($count), true);

        foreach ($data as $key => $value) {
            foreach ($count as $index => $items) {
                if ($value['system_id'] == $items['id']) {
                    $data[$key]['count'] = $items['count'];
                    break;
                } else {
                    $data[$key]['count'] = 0;
                }
            }
        }

        return $data;
    }

    public function create() {
        $id = $_REQUEST['id'];
        $maxprice = $_REQUEST['maxprice'];
        $minprice = $_REQUEST['minprice'];
        $data = DB::table('cars_price')
                    ->where('id', $id)
                    ->get();
        if($data) {
            $data = DB::table('cars_price')->where('id', $id)->update(
                ['max_price' => $maxprice, 'min_price' => $minprice]
            );
        } else {
            $data = DB::table('cars_price')->insert(
                ['id' => $id, 'max_price' => $maxprice, 'min_price' => $minprice]
            );
        }

        if($data) {
            return ['status' == 200, 'mas' => '价格创建成功'];
        } else {
            return ['status' == 400, 'mas' => '价格创建失败'];
        }
    }

    public function delete() {
        $id = $_REQUEST['id'];
        $data = DB::table('cars_price')->where('id', $id)->update(
            ['max_price' => 0, 'min_price' => 0]
        );
        if($data) {
            return ['status' == 200, 'mas' => '价格修改成功'];
        } else {
            return ['status' == 400, 'mas' => '价格修改失败'];
        }
    }

    public function update() {
        $id = $_REQUEST['id'];
    }

    public function abnormal($id) {
        // $data = DB::table('website_price_errors')
        //     ->where('id', $id)
        //     ->get();
        // return $data;

        $data = DB::select('select error.* 
                            from website_price_errors as error 
                            LEFT JOIN cars_price as price on error.id = price.id 
                            where price.id = '.$id.' 
                            and (error.maxprice > price.max_price or error.minprice < price.min_price)');
        return $data;
    }

    public function whole() {
        $users = DB::table('website_price_errors')
                    ->leftJoin('cars_price', 'website_price_errors.id', '=', 'cars_price.id')
                    ->where('website_price_errors.maxprice','>','cars_price.max_price')
                    ->orWhere('website_price_errors.minprice','<','cars_price.min_price')
                    ->orderBy('website_price_errors.id', 'asc')
                    ->get();
        return $users;
    }
}